package org.eredlab.g4.rif.report.excel;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eredlab.g4.ccl.util.GlobalConstants;
import org.eredlab.g4.ccl.util.G4Utils;

/**
 * 导出Excel的模板对象
 * 
 * @author XiongChun
 * @since 2010-08-12
 */
public class ExcelTemplate {

	private Log log = LogFactory.getLog(ExcelTemplate.class);

	private List staticObject = null;
	private List parameterObjct = null;
	private List fieldObjct = null;
	private List variableObject = null;
	private String templatePath = null;

	private List[] staticObjects = null;
	private List[] parameterObjcts = null;
	private List[] fieldObjcts = null;
	private List[] variableObjects = null;
	


	public ExcelTemplate(String pTemplatePath) {
		templatePath = pTemplatePath;
	}
	
	public ExcelTemplate() {
	}
	
	/**
	 * 解析excel模板
	 * @param request
	 */
	public void parseForMultipleSheet(HttpServletRequest request){	
		if(G4Utils.isEmpty(templatePath)){
			log.error(GlobalConstants.Exception_Head + "Excel模板路径不能为空!");
		}
		templatePath = request.getSession().getServletContext().getRealPath(templatePath);
		File file = new File(templatePath);
		Workbook workbook = null;
		try {
			workbook = Workbook.getWorkbook(file);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		int sheetsCount = workbook.getSheets().length;
		staticObjects = new ArrayList[sheetsCount];
		parameterObjcts = new ArrayList[sheetsCount];
		fieldObjcts = new ArrayList[sheetsCount];
		variableObjects = new ArrayList[sheetsCount];
		
		for (int i = 0; i < sheetsCount; i++) {
			staticObjects[i] = new ArrayList(); 
			parameterObjcts[i] = new ArrayList();
			fieldObjcts[i] = new ArrayList();
			variableObjects[i] = new ArrayList();
		}
		
		for(int i=0;i<sheetsCount;i++){
			Sheet sheet = workbook.getSheet(i);
			if (G4Utils.isNotEmpty(sheet)) {
				int rows = sheet.getRows();
				for (int k = 0; k < rows; k++) {
					Cell[] cells = sheet.getRow(k);
					for (int j = 0; j < cells.length; j++) {
						String cellContent = cells[j].getContents().trim();
						if (!G4Utils.isEmpty(cellContent)) {
							if (cellContent.indexOf("$P") != -1 || cellContent.indexOf("$p") != -1) {
								parameterObjcts[i].add(cells[j]);
							} else if (cellContent.indexOf("$F") != -1 || cellContent.indexOf("$f") != -1) {
								fieldObjcts[i].add(cells[j]);
							} else if(cellContent.indexOf("$V") != -1 || cellContent.indexOf("$v") != -1) {
								variableObjects[i].add(cells[j]);
							}else {
								staticObjects[i].add(cells[j]);
							}
						}
					}
				}
			} else {
				log.error("模板工作表对象不能为空!");
			}
		}	
	}
	
	/**
	 * 解析Excel模板
	 */
	public void parse(HttpServletRequest request) {
		staticObject = new ArrayList();
		parameterObjct = new ArrayList();
		fieldObjct = new ArrayList();
		variableObject = new ArrayList();
		if(G4Utils.isEmpty(templatePath)){
			log.error(GlobalConstants.Exception_Head + "Excel模板路径不能为空!");
		}
		templatePath = request.getSession().getServletContext().getRealPath(templatePath);
		File file = new File(templatePath);
		Workbook workbook = null;
		try {
			workbook = Workbook.getWorkbook(file);
		} catch (Exception e) {
			e.printStackTrace();
		}
		

			Sheet sheet = workbook.getSheet(0);
			if (G4Utils.isNotEmpty(sheet)) {
				int rows = sheet.getRows();
				for (int k = 0; k < rows; k++) {
					Cell[] cells = sheet.getRow(k);
					for (int j = 0; j < cells.length; j++) {
						String cellContent = cells[j].getContents().trim();
						if (!G4Utils.isEmpty(cellContent)) {
							if (cellContent.indexOf("$P") != -1 || cellContent.indexOf("$p") != -1) {
								parameterObjct.add(cells[j]);
							} else if (cellContent.indexOf("$F") != -1 || cellContent.indexOf("$f") != -1) {
								fieldObjct.add(cells[j]);
							} else if(cellContent.indexOf("$V") != -1 || cellContent.indexOf("$v") != -1) {
								variableObject.add(cells[j]);
							}else {
								staticObject.add(cells[j]);
							}
						}
					}
				}
			} else {
				log.error("模板工作表对象不能为空!");
			}
				
	}

	/**
	 * 增加一个静态文本对象
	 */
	public void addStaticObject(Cell cell) {
		staticObject.add(cell);
	}

	/**
	 * 增加一个参数对象
	 */
	public void addParameterObjct(Cell cell) {
		parameterObjct.add(cell);
	}

	/**
	 * 增加一个字段对象
	 */
	public void addFieldObjct(Cell cell) {
		fieldObjct.add(cell);
	}


	public List getStaticObject() {
		return staticObject;
	}

	public List getParameterObjct() {
		return parameterObjct;
	}

	public List getFieldObjct() {
		return fieldObjct;
	}

	public String getTemplatePath() {
		return templatePath;
	}
	
	public List[] getStaticObjects(){
		return staticObjects;
	}
	
	public List[] getParameterObjcts(){
		return parameterObjcts;
	}
	
	public List[] getFieldObjcts(){
		return fieldObjcts;
	}

	public void setTemplatePath(String templatePath) {
		this.templatePath = templatePath;
	}

	public List getVariableObject() {
		return variableObject;
	}

	public List[] getVariableObjects() {
		return variableObjects;
	}

}
